package DramCore
{
	
	import flash.display.*;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	
	import mx.core.Application;
	import mx.core.UIComponent;
	
	import utils.Trace;
  	
	public class ThingGraphic extends UIComponent
	{
		include "gibswfinclude.as";
		// How many iterations of current animation left to run
		public var _animCount:Number;
		public var _lastFrame:int;
 		protected var _game:ScriptLauncher;
	    protected var _oldUIC:UIComponent;
	    public var _symbolName:String;
     	private var _sortDepth:int; // From back to front
       	protected var _mc:MovieClip;
 	 	private var _tickTimer:Timer;
 	 	protected var _thingType:String;
 	 	public var _onScreen:Boolean; // Things only get ticks when onScreen
     	
		public function ThingGraphic( name:String , swfName:String, defaultSymbolName:String )
		{
			super();
			this.name = name;
			this._thingType = thingType;
			this._oldUIC = null;
			this._game = Application.application._game;
			this._onScreen = false;
  		}
 
		public function createMovieClip(mcName:String):MovieClip {
			var mc:MovieClip;
			if( mcName.slice( mcName.lastIndexOf(".") ) == ".gif"){
				mc = new MovieClip();
				loadSprite( mcName, mc );
			} else {
			}
			return mc;
		}
		private var _pictLdr:Loader = new Loader();
		public function loadSprite( name:String, mc:MovieClip ):void 
		{
			var pictURL:String = name;
			var pictURLReq:URLRequest = new URLRequest(pictURL);
			this._pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded); 
			this._pictLdr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, reportIOError,false,0,true);
 			this._pictLdr.load(pictURLReq);
		}
		private function imgLoaded(e:Event):void
		{
        	var uic:UIComponent = new UIComponent();
    		this.addChild( uic );
      		uic.addChild ( this._pictLdr.content);
  		    this._pictLdr = null;
 		}
		private function reportIOError( event:Event ):void {
			Trace.throwError("movieClipFactory.loadSprite IOError: "+event.toString() );
		}
         public function get symbolName():String{
         	return this._symbolName;
         }	
         // Advance anim a frame. If it loops or is one frame long, return true
         public function nextFrameEndsAnimation():Boolean {
 			trace("nfa : "+this._symbolName);
         	var result:Boolean = false;
	        	var currentFrame:int = this._mc.currentFrame;
	        	for( var i:int = 0; i < this._mc.numChildren; i++){
	        		var mc:MovieClip = this._mc.getChildAt( i ) as MovieClip;
	        		if( mc ){
        			trace(i);
	        			mc.nextFrame();
	        		}
	        	}
	         	if( currentFrame >= this._mc.totalFrames -1 ){
	         		result = true;
	         		this.stopAnim();
	         	}
         	return result;
         }
   		public function animateFrames( frameStart:int, lastFrame:int = -1, 
   			interval:Number = -1, repeat:int = 1 ):void{
   			if( interval == -1){
   				interval = ScriptLauncher.TICKINTERVAL;
   			}
			if( this._mc.framesLoaded > 1){
				if( lastFrame == -1 ){
					this._lastFrame = this._mc.framesLoaded ;
				} else {
					this._lastFrame = lastFrame;
				}
				// If there is more than one frame to play detect the last frame
				if( lastFrame - frameStart > 0){
					this._mc.addEventListener( Event.ENTER_FRAME , detectLastFrame,false,0,true );
				}
				this._mc.gotoAndPlay( frameStart );
			} else {
				this._animCount = 0;
			}
			Trace.myTrace(">>ThingGraphic.animateFrames: "+this.symbolName +" lastFrame: "+this._lastFrame );
		}
		protected function detectLastFrame( e:Event ):void {
//		Trace.myTrace("dlf curFrame: "+e.target.currentFrame +" name: "+e.target.name );
			if( e.target.currentFrame >= this._lastFrame ){
//				Trace.myTrace("dlf stop curFrame: "+e.target.currentFrame +" last: "+this._lastFrame  );
				if( this._animCount > 0){ // Only decrement to 0
					this._animCount--;
				}
				e.target.stop();
				e.target.removeEventListener( Event.ENTER_FRAME, detectLastFrame );
			}
		}

         public function stopAnim():void {
         	this._mc.gotoAndStop(0);
         }
         public function get sortDepth():int {
         	return this._sortDepth;
         }
         public function set sortDepth( depth:int ):void {
         	this._sortDepth = depth;
         }
         public function scale( s:Number ):void {
         	this.scaleY =
         		this.scaleX = s;
         }
         public function setXY( x:Number, y:Number ):void {
         	this.x = x;
         	this.y = y;
         }
         public function get thingType():String {
         	return this._thingType;
         }
         // Offsets the graphic from the origin
         public function offsetXY( x:int, y:int ):void {
         	this._mc.x += x;
         	this._mc.y += y;
         }
         override public function set alpha( val:Number ):void {
         	super.alpha = val;
         	if( this._mc )
         		this._mc.alpha = val;
         }
	}
}